package com.iknow.xmpp.service;

import android.app.PendingIntent;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.RemoteCallbackList;
import android.os.RemoteException;
import android.preference.PreferenceManager;
import com.iknow.activity.XmppChatActivity;
import com.iknow.util.Loger;
import com.iknow.xmpp.service.aidl.IChat;
import com.iknow.xmpp.service.aidl.IChatManager.Stub;
import com.iknow.xmpp.service.aidl.IChatManagerListener;
import com.iknow.xmpp.service.aidl.IMessageListener;
import com.iknow.xmpp.service.aidl.IMessageListener.Stub;
import com.iknow.xmpp.service.aidl.IRoster;
import com.iknow.xmpp.service.aidl.IXmppFacade;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.ChatManager;
import org.jivesoftware.smack.ChatManagerListener;

public class IKnowChatManager extends IChatManager.Stub
{
  private static final String TAG = "IKnowChatManager";
  private final ChatManager mAdaptee;
  private final ChatListener mChatListener;
  private final Map<String, ChatAdapter> mChats = new HashMap();
  private final RemoteCallbackList<IChatManagerListener> mRemoteChatCreationListeners;
  private final IKnowXmppService mService;

  public IKnowChatManager(ChatManager paramChatManager, IKnowXmppService paramIKnowXmppService)
  {
    this.mChatListener = new ChatListener(this);
    this.mRemoteChatCreationListeners = new RemoteCallbackList();
    this.mService = paramIKnowXmppService;
    this.mAdaptee = paramChatManager;
    this.mAdaptee.addChatListener(this.mChatListener);
  }

  private ChatAdapter getChat(Chat paramChat)
  {
    Object localObject;
    String str = paramChat.getParticipant();
    if (this.mChats.containsKey(str))
      localObject = (ChatAdapter)this.mChats.get(str);
    while (true)
    {
      return localObject;
      ChatAdapter localChatAdapter = new ChatAdapter(paramChat);
      localChatAdapter.setAccountUser(PreferenceManager.getDefaultSharedPreferences(this.mService.getBaseContext()).getString("account_username", ""));
      this.mChats.put(str, localChatAdapter);
      localObject = localChatAdapter;
    }
  }

  public void addChatCreationListener(IChatManagerListener paramIChatManagerListener)
    throws RemoteException
  {
    if (paramIChatManagerListener != null)
      this.mRemoteChatCreationListeners.register(paramIChatManagerListener);
  }

  public IChat createChat(Contact paramContact, IMessageListener paramIMessageListener)
    throws RemoteException
  {
    return createChat(paramContact.getJIDWithRes(), paramIMessageListener);
  }

  public IChat createChat(String paramString, IMessageListener paramIMessageListener)
  {
    Object localObject;
    ChatAdapter localChatAdapter2;
    if (this.mChats.containsKey(paramString))
      localChatAdapter2 = (ChatAdapter)this.mChats.get(paramString);
    try
    {
      localChatAdapter2.addMessageListener(paramIMessageListener);
      localObject = localChatAdapter2;
      return localObject;
    }
    catch (RemoteException localRemoteException2)
    {
      while (true)
        localRemoteException2.printStackTrace();
      ChatAdapter localChatAdapter1 = getChat(this.mAdaptee.createChat(paramString, null));
      try
      {
        localChatAdapter1.addMessageListener(paramIMessageListener);
        localObject = localChatAdapter1;
      }
      catch (RemoteException localRemoteException1)
      {
        while (true)
          localRemoteException1.printStackTrace();
      }
    }
  }

  public void deleteChatNotification(IChat paramIChat)
    throws RemoteException
  {
    try
    {
      this.mService.deleteNotification(paramIChat.getParticipant().getJID().hashCode());
      return;
    }
    catch (RemoteException localRemoteException)
    {
      Loger.v("IKnowChatManager", "Remote exception ", localRemoteException);
    }
  }

  public void destroyChat(IChat paramIChat)
    throws RemoteException
  {
    if (paramIChat == null);
    while (true)
    {
      return;
      deleteChatNotification(paramIChat);
      this.mChats.remove(paramIChat.getParticipant().getJID());
    }
  }

  public ChatAdapter getChat(Contact paramContact)
  {
    String str = paramContact.getJIDWithRes();
    return ((ChatAdapter)this.mChats.get(str));
  }

  public List<Contact> getOpenedChatList()
    throws RemoteException
  {
    ArrayList localArrayList = new ArrayList();
    IRoster localIRoster = this.mService.getBind().getRoster();
    Iterator localIterator = this.mChats.values().iterator();
    while (true)
    {
      ChatAdapter localChatAdapter;
      do
      {
        if (!(localIterator.hasNext()))
          return localArrayList;
        localChatAdapter = (ChatAdapter)localIterator.next();
      }
      while (localChatAdapter.getMessages().size() <= 0);
      Contact localContact = localIRoster.getContact(localChatAdapter.getParticipant().getJID());
      if (localContact == null)
        localContact = new Contact(localChatAdapter.getParticipant().getJID());
      localArrayList.add(localContact);
    }
  }

  public void removeChatCreationListener(IChatManagerListener paramIChatManagerListener)
    throws RemoteException
  {
    if (paramIChatManagerListener != null)
      this.mRemoteChatCreationListeners.unregister(paramIChatManagerListener);
  }

  private class ChatListener extends IMessageListener.Stub
  implements ChatManagerListener
  {
    private PendingIntent makeChatIntent(, Friend paramFriend)
    {
      Intent localIntent = new Intent(IKnowChatManager.access$4(this.this$0), XmppChatActivity.class);
      localIntent.setFlags(805437440);
      try
      {
        localIntent.putExtra("friend_jid", paramFriend.getID());
        localIntent.putExtra("name", paramFriend.getName());
        localIntent.putExtra("contact", paramIChat.getParticipant());
        return PendingIntent.getActivity(IKnowChatManager.access$4(this.this$0), 0, localIntent, 134217728);
      }
      catch (RemoteException localRemoteException)
      {
        Loger.e("IKnowChatManager", localRemoteException.getMessage());
      }
    }

    // ERROR //
    private void notifyNewChat(, String paramString)
    {
      // Byte code:
      //   0: getstatic 92	com/iknow/IKnow:mIKnowDataBase	Lcom/iknow/db/IKnowDataBase;
      //   3: aload_1
      //   4: invokeinterface 63 1 0
      //   9: invokevirtual 97	com/iknow/xmpp/service/Contact:getJID	()Ljava/lang/String;
      //   12: invokevirtual 103	com/iknow/db/IKnowDataBase:getFriendInfo	(Ljava/lang/String;)Lcom/iknow/xmpp/service/Friend;
      //   15: astore 5
      //   17: aload 5
      //   19: ifnull +88 -> 107
      //   22: aload 5
      //   24: invokevirtual 55	com/iknow/xmpp/service/Friend:getName	()Ljava/lang/String;
      //   27: pop
      //   28: aload 5
      //   30: invokevirtual 55	com/iknow/xmpp/service/Friend:getName	()Ljava/lang/String;
      //   33: astore 8
      //   35: new 105	android/app/Notification
      //   38: dup
      //   39: ldc 106
      //   41: aload 8
      //   43: invokestatic 112	java/lang/System:currentTimeMillis	()J
      //   46: invokespecial 115	android/app/Notification:<init>	(ILjava/lang/CharSequence;J)V
      //   49: astore 9
      //   51: aload 9
      //   53: bipush 16
      //   55: putfield 119	android/app/Notification:flags	I
      //   58: aload 9
      //   60: aload_0
      //   61: getfield 15	com/iknow/xmpp/service/IKnowChatManager$ChatListener:this$0	Lcom/iknow/xmpp/service/IKnowChatManager;
      //   64: invokestatic 28	com/iknow/xmpp/service/IKnowChatManager:access$4	(Lcom/iknow/xmpp/service/IKnowChatManager;)Lcom/iknow/xmpp/service/IKnowXmppService;
      //   67: aload 8
      //   69: aload_2
      //   70: aload_0
      //   71: aload_1
      //   72: aload 5
      //   74: invokespecial 121	com/iknow/xmpp/service/IKnowChatManager$ChatListener:makeChatIntent	(Lcom/iknow/xmpp/service/aidl/IChat;Lcom/iknow/xmpp/service/Friend;)Landroid/app/PendingIntent;
      //   77: invokevirtual 125	android/app/Notification:setLatestEventInfo	(Landroid/content/Context;Ljava/lang/CharSequence;Ljava/lang/CharSequence;Landroid/app/PendingIntent;)V
      //   80: aload_0
      //   81: getfield 15	com/iknow/xmpp/service/IKnowChatManager$ChatListener:this$0	Lcom/iknow/xmpp/service/IKnowChatManager;
      //   84: invokestatic 28	com/iknow/xmpp/service/IKnowChatManager:access$4	(Lcom/iknow/xmpp/service/IKnowChatManager;)Lcom/iknow/xmpp/service/IKnowXmppService;
      //   87: aload_1
      //   88: invokeinterface 63 1 0
      //   93: invokevirtual 97	com/iknow/xmpp/service/Contact:getJID	()Ljava/lang/String;
      //   96: invokevirtual 131	java/lang/String:hashCode	()I
      //   99: aload 9
      //   101: invokevirtual 137	com/iknow/xmpp/service/IKnowXmppService:sendNotification	(ILandroid/app/Notification;)V
      //   104: goto +129 -> 233
      //   107: aload_0
      //   108: getfield 15	com/iknow/xmpp/service/IKnowChatManager$ChatListener:this$0	Lcom/iknow/xmpp/service/IKnowChatManager;
      //   111: invokestatic 28	com/iknow/xmpp/service/IKnowChatManager:access$4	(Lcom/iknow/xmpp/service/IKnowChatManager;)Lcom/iknow/xmpp/service/IKnowXmppService;
      //   114: invokevirtual 141	com/iknow/xmpp/service/IKnowXmppService:getBind	()Lcom/iknow/xmpp/service/aidl/IXmppFacade;
      //   117: invokeinterface 147 1 0
      //   122: invokeinterface 153 1 0
      //   127: astore 6
      //   129: aload 6
      //   131: ifnull +95 -> 226
      //   134: aload 6
      //   136: aload_1
      //   137: invokeinterface 63 1 0
      //   142: invokevirtual 97	com/iknow/xmpp/service/Contact:getJID	()Ljava/lang/String;
      //   145: invokestatic 159	org/jivesoftware/smack/util/StringUtils:parseName	(Ljava/lang/String;)Ljava/lang/String;
      //   148: invokeinterface 164 2 0
      //   153: astore 12
      //   155: aload 12
      //   157: astore 7
      //   159: aload 7
      //   161: ifnonnull +58 -> 219
      //   164: new 42	com/iknow/xmpp/service/Friend
      //   167: dup
      //   168: aload_1
      //   169: invokeinterface 63 1 0
      //   174: invokespecial 167	com/iknow/xmpp/service/Friend:<init>	(Lcom/iknow/xmpp/service/Contact;)V
      //   177: astore 5
      //   179: getstatic 92	com/iknow/IKnow:mIKnowDataBase	Lcom/iknow/db/IKnowDataBase;
      //   182: aload 5
      //   184: invokevirtual 171	com/iknow/db/IKnowDataBase:addFriend	(Lcom/iknow/xmpp/service/Friend;)V
      //   187: goto -159 -> 28
      //   190: astore_3
      //   191: aload_3
      //   192: astore 4
      //   194: ldc 75
      //   196: aload 4
      //   198: invokevirtual 78	android/os/RemoteException:getMessage	()Ljava/lang/String;
      //   201: invokestatic 84	com/iknow/util/Loger:e	(Ljava/lang/String;Ljava/lang/String;)V
      //   204: goto +29 -> 233
      //   207: astore 10
      //   209: aload 10
      //   211: astore 4
      //   213: aload 7
      //   215: pop
      //   216: goto -22 -> 194
      //   219: aload 7
      //   221: astore 5
      //   223: goto -44 -> 179
      //   226: aload 5
      //   228: astore 7
      //   230: goto -71 -> 159
      //   233: return
      //
      // Exception table:
      //   from	to	target	type
      //   0	155	190	android/os/RemoteException
      //   179	187	190	android/os/RemoteException
      //   164	179	207	android/os/RemoteException
    }

    public void chatCreated(, boolean paramBoolean)
    {
      ChatAdapter localChatAdapter = IKnowChatManager.access$1(this.this$0, paramChat);
      Loger.d("IKnowChatManager", "Chat" + paramChat.toString() + " created locally " + paramBoolean + " with " + paramChat.getParticipant());
      try
      {
        localChatAdapter.addMessageListener(IKnowChatManager.access$2(this.this$0));
        int i = IKnowChatManager.access$3(this.this$0).beginBroadcast();
        int j = 0;
        if (j >= i)
        {
          IKnowChatManager.access$3(this.this$0).finishBroadcast();
        }
        else
        {
          ((IChatManagerListener)IKnowChatManager.access$3(this.this$0).getBroadcastItem(j)).chatCreated(localChatAdapter, paramBoolean);
          ++j;
        }
      }
      catch (RemoteException localRemoteException)
      {
        Loger.w("IKnowChatManager", " Error while triggering remote connection listeners in chat creation", localRemoteException);
      }
    }

    public void processMessage(, Message paramMessage)
    {
      String str;
      try
      {
        str = paramMessage.getBody();
        if ((!(paramIChat.isOpen())) && (str != null))
        {
          if (paramIChat instanceof ChatAdapter)
            IKnowChatManager.access$5(this.this$0).put(paramIChat.getParticipant().getJID(), (ChatAdapter)paramIChat);
          notifyNewChat(paramIChat, str);
        }
        return;
      }
      catch (RemoteException localRemoteException)
      {
        Loger.e("IKnowChatManager", localRemoteException.getMessage());
      }
    }

    public void stateChanged()
    {
    }
  }
}